package visual;


import java.text.SimpleDateFormat;
import java.util.*;
import java.io.*;

import org.apache.poi.hssf.util.HSSFColor;

import jminhep.cluster.DataHolder;
import jminhep.cluster.DataPoint;

import data.MatrixTime;
import utils.kml.*;



public class KMLPrinterCluster {
	public static Map<Integer,String> colorMapping;
	
	
	

	public static void printMap(MatrixTime erlang, DataHolder data, String[][] labels, String out_file, String COLOR_FILE) {	
		
		try {
			
			readColor(COLOR_FILE);
			
			
			String kml_file = new String(out_file);
			
			Kml utilsKml = new Kml();
			
			
			PrintWriter out = new PrintWriter(new FileWriter(new File(kml_file)));
			utilsKml.printHeaderFolder(out, out_file.substring(out_file.indexOf("/")+1));			
			 
			printMap(erlang, data, labels, out);
			
            utilsKml.printFooterFolder(out);
			out.close();
			
			
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void readColor(String file){
		colorMapping = new HashMap<Integer,String>();
		
		try{
		
			BufferedReader br = new BufferedReader(new FileReader(new File(file)));
			String line = "";
			int counter = 0;
			while((line = br.readLine()) != null){
				colorMapping.put(counter, line.trim());
				counter++;
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	
	public static final SimpleDateFormat FORMATTER = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
	
	private static void printMap(MatrixTime mt, DataHolder data, String[][] labels, PrintWriter out) throws Exception {	
		
		out.println("<Folder>");
		out.println("<name>"+FORMATTER.format(mt.getStart().getTime())+"-"+FORMATTER.format(mt.getEnd().getTime())+"</name>");
		
		KMLSquare kml_square = new KMLSquare();
		
		int counter = 0;
		for(int i=0; i<mt.getMat().length; i++)
		for(int j=0; j<mt.getMat()[i].length;j++){
			DataPoint dp = data.getRaw(counter);
			out.println(printSquare(dp.getClusterNumber(), mt, i, j, kml_square,labels));
			counter++;
		}
		
		out.println("</Folder>");
	}
	
	private static String printSquare(int cluster, MatrixTime mt, int i, int j, KMLSquare kml_square, String[][] labels) {
		double[][] ll = mt.getArea().getCellBorder(i, j);
		String name = "("+i+","+j+") = "+cluster;
		String transparent = "aa";
		
		//the pixel is not assigned to any cluster, color fully opaque
		if(cluster == -1)
			transparent = "ff";
		
		String color = transparent+colorMapping.get(cluster);

		
		return kml_square.draw(ll, name, color, color, plot(labels[i][j]));
	}
	
	private static String plot(String trend){
		String l_color = "990066";
		String l_size = "3";
		
		int pieces = trend.split(",").length;
		String div = "";
		if(pieces > 24)
			div = "&chm=V,FFFF00,0,23,2.0";
		
	    return "<img src=\"http://chart.apis.google.com/chart?chxr=0,0,1|1,0,"+pieces+"&chxt=y,x&chs=300x225&cht=lc&chco="+l_color+"&chds=-1,1&chd=t:"+trend+div+"&chg=14.3,-1,1,1&chls="+l_size+"\" width=\"300\" height=\"225\" alt=\"\" />";

	}
}
